//
//  RootVCViewController.m
//  BaseFramework01
//
//  Created by IMAC on 2017/6/1.
//  Copyright © 2017年 IMAC. All rights reserved.
//

#import "RootVC.h"
#import "BaseColumnModel.h"
#import "RecommendVC.h"
#import "LiveVC.h"
#import "OnDemandVC.h"
#import "LiveVC.h"
#import "LocalVC.h"
@interface RootVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *headerScrollview;
@property (nonatomic, strong) NSMutableArray *columns;
@property (nonatomic, strong) NSArray *columnModels;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIScrollView *mainScrollView;




@end

@implementation RootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(245, 245, 245);
    
    UIImageView * logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 38, 100, 40)];
    logoImageView.image = [UIImage imageNamed:@"my_icon_about_logo"];
    [self.view addSubview:logoImageView];
    
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(CGRectGetMaxX(logoImageView.frame)+15, 35, SCREEN_WIDTH - CGRectGetMaxX(logoImageView.frame)-15 -  15, 44)];
    searchBar.backgroundColor = RGB(245, 245, 245);
//    [searchBar setBackgroundImage:[UIImage new]];
    searchBar.barTintColor = RGB(245, 245, 245);
    searchBar.tintColor = RGB(245, 245, 245);
    searchBar.placeholder = @"小别离";
    [searchBar.layer setCornerRadius:searchBar.height*0.5];
    for (UIView *subview in [[searchBar.subviews firstObject] subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
        }
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
            [subview.layer setCornerRadius:subview.height*0.5];
            subview.clipsToBounds = YES;
        }
    }
    [self.view addSubview:searchBar];
    
    __block UIScrollView *headerScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(searchBar.frame), SCREEN_WIDTH - 150, 32)];
//    headerScrollview.backgroundColor = [UIColor redColor];
    _headerScrollview = headerScrollview;
    [self.view addSubview:headerScrollview];

    
    CGFloat btnWidth = 30.0;
    CGFloat spacing = (SCREEN_WIDTH - headerScrollview.width -btnWidth*3-30)/2.0;
    for (int i = 0; i<3; i++) {
        UIButton *button  =[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetMaxX(headerScrollview.frame)+15 +i*(btnWidth+spacing),  CGRectGetMaxY(searchBar.frame), btnWidth, btnWidth);
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"cache%d",i+1]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"cache%d_selected",i+1]] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100+i;
        [self.view addSubview:button];
        
    }
 
    
    
    
    
    [BaseColumnModel requestForHomeColumnListDataWithResult:^(NSMutableArray *array) {
        CGFloat spacing = 15.0;
        CGFloat btnWidth = 38.0;
        CGFloat btnHeight = 22.0;
        _columns = [NSMutableArray arrayWithCapacity:array.count];
        _columnModels = array;
        
        UIScrollView *mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, MaxY(headerScrollview), SCREEN_WIDTH, SCREEN_HEIGHT - MaxY(headerScrollview) - UI_TAB_BAR_HEIGHT)];
        mainScrollView.pagingEnabled = YES;
        mainScrollView.delegate = self;
        _mainScrollView  = mainScrollView;
        for (int i = 0; i<array.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
            button.frame = CGRectMake(spacing +i*(btnWidth +spacing), (headerScrollview.height - btnHeight)*0.5, btnWidth, btnHeight);
            BaseColumnModel *model = array[i];
            [button setTitle:model.columnName forState:UIControlStateNormal];
            button.titleLabel.font = FONT_OF_SIZE(17);
            [button setTitleColor:RGB(0, 29, 48) forState:UIControlStateNormal];
            [button setTitleColor:RGB(249, 131, 17) forState:UIControlStateSelected];
//            button.backgroundColor = [UIColor redColor];
            button.tag = i;
            [headerScrollview addSubview:button];
            [_columns addObject:button];
            [button addTarget:self action:@selector(columnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            BaseViewController *baseVC = nil;
            switch (i) {
                case 0:
                {
                    button.selected = YES;
                    baseVC = [[RecommendVC alloc]init];
                }
                    break;
                case 1:{
                    baseVC = [[LiveVC alloc]init];
                }
                    
                    break;
                case 2:
                {
                    baseVC = [[OnDemandVC alloc]init];
                    
                }
                    
                    break;
                case 3:
                {
                    baseVC = [[LocalVC alloc]init];
                }
                    
                    break;
                default:
                    break;
            }
            if (baseVC) {
                baseVC.BaseModel = array[i];
                baseVC.startY = MaxY(headerScrollview);
                [self addChildViewController:baseVC];
                baseVC.view.frame = CGRectMake(i*SCREEN_WIDTH, 0, mainScrollView.width, mainScrollView.height);
                [mainScrollView addSubview:baseVC.view];
            }
            
            
        }
        
        headerScrollview.contentSize = CGSizeMake(spacing *2 +btnWidth * array.count, headerScrollview.height);
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(spacing, headerScrollview.height - 2.0, btnWidth, 2.0)];
        lineView.backgroundColor = RGB(248, 126, 0);
        [headerScrollview addSubview:lineView];
        _lineView = lineView;
        
        mainScrollView.contentSize = CGSizeMake(mainScrollView.width * array.count, mainScrollView.height);
        [self.view addSubview:mainScrollView];
        
        
        

    } failure:^{
        
    }];
    

    
}



-(void)headerBtnClick:(UIButton *)sender
{
    NSLog(@"buttonClicked");
}
-(void)columnBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    
    for (UIButton *btn in _columns) {
        if (btn != sender) {
            btn.selected = NO;
        }
    }
    [UIView animateWithDuration:0.3 animations:^{
        _mainScrollView.contentOffset = CGPointMake(_mainScrollView.width *sender.tag, 0)
        ;
    }];
   
    
}

#pragma MARK UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scale = (_lineView.width +15)/scrollView.width;
    CGFloat offsetX = scrollView.contentOffset.x*scale;
    
    int index = (int)scrollView.contentOffset.x/scrollView.width;
    UIButton *selectBtn = _columns[index];
    for (UIButton *btn in _columns) {
        if (btn != selectBtn) {
            btn.selected = NO;
        }else{
            btn.selected = YES;
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
    }];
    
}

@end
